<?php 
include('init.php');

/**
 * Exit with 404 code 
 */
function exit404()
{
	// Exit wit 404 code
	header("HTTP/1.0 404 Not Found");
	exit();
}
/**
 * Exit with 403 code 
 */
function exit403()
{
	header("HTTP/1.0 403 Permission denied");
	exit();
}
	
	$param = explode('/', $_SERVER['REQUEST_URI']);
	while ( $param[0] == '' ){
		array_shift($param);
	}
	/*
	 * If not called in /cs/
	 * Check $param[0] as 'cs'
	 */
	if ( strtolower($param[0]) != 'cs' )
	{
		exit404();
	}
	/*
	 * Check 
	 * 	$param[1] as client_key
	 * 	$param[2] as article_key
	 */
	if ( !isset($param[1]) || !isset($param[2]) )
	{
		exit404();	
	}
	$clientKey = $param[1];
	$articleKey = $param[2];
	/*
	 * Check in db
	 */
	$isRegisteredClient = $db->clients
		->find(array('client_key' => $clientKey))
		->count();
	/* 
	 * Exit if not exactly 1 client_key existed, 
	 * 	Error or not found.  
	 */
	if ( $isRegisteredClient != 1 ) 
	{
		exit403();
	}
	/*
	 * Take action 
	 */
	$comments = $db->comments;
	switch (strtolower($_SERVER['REQUEST_METHOD'])) {
		case 'get':
			if ( !isset($param[3]) )
			{
				exit404();
			}
			
			$result = Array();
			switch (strtolower($param[3])) {
				case 'page':
					$result['type'] = 'page';
					$result['first'] = DEFAULT_PAGE_VIEW;
					$result['pageSize'] = DEFAULT_PAGE_SIZE;
					if (isset($param[4]))
					{
						$result['first'] = intval($param[4]);
					}
					if (isset($param[5]))
					{
						$result['pageSize'] = intval($param[5]);
					}
					$articleComments = $comments->find(
						array(
							'client_key' => $clientKey,
							'article_key' => $articleKey
						))
						->limit($result['pageSize'])
						->skip($result['first']-1);
				break;
				default:
					$result['type'] = 'all';
					$articleComments = $comments->find(
						array(
							'client_key' => $clientKey,
							'article_key' => $articleKey
						));
				break;
			}
			
			$result['total'] = $comments->find()->count();
			$result['data'] = Array();
			foreach ($articleComments as $articleComment) {
				$item = Array(
					'client_key' => $articleComment['client_key'],
					'article_key' => $articleComment['article_key'],
					'date' => date("Y M d, H:i:s",$articleComment['date']->sec),
					'author' => $articleComment['author'],
					'content' => $articleComment['content']
				);
				$result['data'][] = $item;
			}

			echo json_encode($result); 
		;
		break;
		case 'post':
			if ( !isset($param[3]) // not received 'author' param
				|| isset($param[4]) ) // get more than 'author'
			{
				exit404();
			}
			$author = $param[3];
			/*
			 * Add !
			 */
			$comment = array(
				'client_key' => $clientKey,
				'article_key' => $articleKey,
				'date' => new MongoDate(strtotime("now")),
				'author' => $author,
				'content' => $HTTP_RAW_POST_DATA
			);
			$comments->insert($comment);
		;
		break;
		case 'delete':
			/*
			 * Not supportted yet 
			 */
			exit403();
		;
		break;
		case 'put':
			/*
			 * Not supportted yet 
			 */
			exit403();
		;
		break;
		default:
			exit404();
	}
?>
